home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1989 / 03b / winvue / winvuems.c < prev    next >
C/C++ Source or Header  |  1989-02-14  |  9KB  |  297 lines

  1. /* 
  2.   Winvue file open routines.
  3.  
  4.   Most of the routines here are taken from the Microsoft TEMPLATE
  5.   Application.  Minor adaptations have been made to suit the
  6.   Winvue program.
  7. */
  8.  
  9. #define NOCOMM        
  10. #define NOKANJI
  11. #define NOATOM
  12. #define NOBITMAP
  13. #define NOMINMAX
  14. #include <windows.h>
  15. #include "longstr.h"
  16. #include "winvue.h"
  17.  
  18. /* local function declarations */
  19. static VOID NEAR DlgCheckOkEnable (HWND, int, unsigned);
  20. static void NEAR DlgAddCorrectExtension (char *, BOOL);
  21. static BOOL NEAR DlgCheckFilename (char *);
  22. static BOOL NEAR DlgSearchSpec (char *);
  23.  
  24. #define ATTRDIRLIST 0x4010  /* include directories and drives in listbox */
  25.  
  26. static char *szExtSave = "\\*.TXT";    /* default extension */
  27. int cbRootNameMax = MAX_FNAME_LEN -CBEXTMAX - 1;
  28.     
  29. /* This is the window proc for the about box when it is displayed */
  30. BOOL FAR PASCAL AboutBoxProc(hDlg, message, wParam, lParam)
  31. HWND hDlg;
  32. unsigned message;
  33. WORD wParam;
  34. LONG lParam;
  35. {
  36.  
  37.     switch (message) {
  38.  
  39.       /* nothing to initialize */
  40.     case WM_INITDIALOG:
  41.         break;
  42.  
  43.       /* this dialog box has only an OK button */
  44.     case WM_COMMAND:
  45.         switch (wParam) {
  46.         case IDOK:
  47.         /* destroy the dialog box */
  48.             EndDialog(hDlg,TRUE);
  49.             break;
  50.         default:
  51.             return FALSE;        /* we did not process */
  52.         }
  53.         break;
  54.         
  55.     default:
  56.         return FALSE;
  57.     }
  58.     return TRUE;        /* we processed message */
  59. }
  60.  
  61. /*=============================================================================
  62.  DLGFNOPEN controls the Open dialog box.  The dialog box handles input, allows
  63.  user to change directories, checks for legal filenames, opens specified files,
  64.  appends default extension if needed, and returns the file's name and ofstruct.
  65.  This routine correctly parses filenames containing KANJI characters.
  66. =============================================================================*/
  67. int FAR PASCAL DlgFnOpen(hwnd, msg, wParam, lParam)
  68. HWND hwnd;
  69. unsigned msg;
  70. WORD wParam;
  71. LONG lParam;
  72. {
  73.  
  74.     char rgch[256];
  75.     int cchFile, cchDir;
  76.     char *pchFile;
  77.     BOOL    fWild;
  78.  
  79.     switch (msg) {
  80.     case WM_INITDIALOG:
  81.     /* Set edit field with default search spec */
  82.     SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)(szExtSave+1));
  83.     /* Don't let user type more than cbRootNameMax bytes in edit ctl. */
  84.     SendDlgItemMessage(hwnd, IDD_EDIT, EM_LIMITTEXT, cbRootNameMax, 0L);
  85.  
  86.     /* fill list box with filenames that match spec, and fill static
  87.        field with path name */
  88.     if (!DlgDirList(hwnd,(LPSTR)(szExtSave+1),
  89.                     IDD_LISTBOX,IDD_PATH,ATTRDIRLIST))
  90.         EndDialog(hwnd, FALSE);
  91.     break;
  92.  
  93.     case WM_COMMAND:
  94.     switch (wParam) {
  95.     case IDOK:
  96.         if (IsWindowEnabled(GetDlgItem(hwnd, IDOK))) {
  97.         /* Get contents of edit field */
  98.         /* Add search spec if it does not contain one. */
  99.         GetDlgItemText(hwnd,IDD_EDIT,(LPSTR)szFileNameSave,
  100.                                 cbRootNameMax);
  101.         lstrcpy((LPSTR)rgch, (LPSTR)szFileNameSave);
  102.  
  103.         /* Append appropriate extension to user's entry */
  104.         DlgAddCorrectExtension(rgch, TRUE);
  105.         /* Try to open directory.  If successful, fill listbox with
  106.            contents of new directory.  Otherwise, open datafile. */
  107.         if (DlgSearchSpec(rgch)) {
  108.             if (DlgDirList(hwnd, (LPSTR)rgch, 
  109.                     IDD_LISTBOX, IDD_PATH, ATTRDIRLIST)){
  110.             lstrcpy((LPSTR)szFileNameSave, (LPSTR)rgch);
  111.             SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)szFileNameSave);
  112.             break;
  113.             }
  114.         }
  115.  
  116.         DlgAddCorrectExtension(szFileNameSave, FALSE);
  117.         /* If no directory list and filename contained search spec,
  118.            honk and don't try to open. */
  119.         if (DlgSearchSpec(szFileNameSave)) {
  120.             MessageBeep(0);
  121.             break;
  122.         }
  123. LoadIt:
  124.         /* Make filename upper case and if it's a legal dos
  125.            name, try to open the file. */
  126.         AnsiUpper((LPSTR)szFileNameSave);
  127.         if (!DlgCheckFilename(szFileNameSave)) {
  128.             /* illegal filename */
  129.             ShowMessage(hwnd, szFileNameSave, IDS_IFN,
  130.                 MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL);
  131.             break;
  132.         }
  133.         EndDialog(hwnd, TRUE);
  134.         break;
  135.         }
  136.         break;
  137.  
  138.     case IDCANCEL:
  139.         /* User pressed cancel.  Just take down dialog box. */
  140.         EndDialog(hwnd, FALSE);
  141.         break;
  142.  
  143.     /* User single clicked or doubled clicked in listbox -
  144.        Single click means fill edit box with selection.
  145.        Double click means go ahead and open the selection. */
  146.     case IDD_LISTBOX:
  147.         switch (HIWORD(lParam)) {
  148.  
  149.         /* Single click case */
  150.         case 1:
  151.         GetDlgItemText(hwnd, IDD_EDIT, (LPSTR)rgch, cbRootNameMax);
  152.  
  153.         /* Get selection, which may be either a prefix to a new search
  154.            path or a filename. DlgDirSelect parses selection, and
  155.            appends a backslash if selection is a prefix */
  156.         if (DlgDirSelect(hwnd, (LPSTR)szFileNameSave, IDD_LISTBOX)) {
  157.             cchDir = lstrlen((LPSTR)szFileNameSave);
  158.             cchFile = lstrlen((LPSTR)rgch);
  159.             pchFile = rgch+cchFile;
  160.  
  161.             /* Now see if there are any wild characters (* or ?) in
  162.                edit field.  If so, append to prefix. If edit field
  163.                contains no wild cards append default search spec
  164.                which is  "*.TXT" for notepad. */
  165.             fWild = (*pchFile == '*' || *pchFile == ':');
  166.             while (pchFile > rgch) {
  167.             pchFile = (char *)AnsiPrev((LPSTR)(rgch),
  168.                                 (LPSTR)pchFile);
  169.             if (*pchFile == '*' || *pchFile == '?')
  170.                 fWild = TRUE;
  171.             if (*pchFile == '\\' || *pchFile == ':') {
  172.                 pchFile = (char *)AnsiNext((LPSTR)pchFile);
  173.                 break;
  174.             }
  175.             }
  176.             if (fWild)
  177.             lstrcpy((LPSTR)szFileNameSave + cchDir, (LPSTR)pchFile);
  178.             else
  179.             lstrcpy((LPSTR)szFileNameSave + cchDir,
  180.                             (LPSTR)(szExtSave+1));
  181.         }
  182.  
  183.         /* Set edit field to entire file/path name. */
  184.         SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)szFileNameSave);
  185.         break;
  186.  
  187.         /* Double click case - first click has already been processed
  188.            as single click */
  189.         case 2:
  190.         /* Basically the same as ok.  If new selection is directory,
  191.            open it and list it.  Otherwise, open file. */
  192.         if (DlgDirList(hwnd, (LPSTR)szFileNameSave, 
  193.                 IDD_LISTBOX, IDD_PATH, ATTRDIRLIST)) {
  194.             SetDlgItemText(hwnd, IDD_EDIT, (LPSTR)szFileNameSave);
  195.             break;
  196.         }
  197.         goto LoadIt;    /* go load it up */
  198.         }
  199.         break;
  200.  
  201.     case IDD_EDIT:
  202.         DlgCheckOkEnable(hwnd, IDD_EDIT, HIWORD(lParam));
  203.         break;
  204.  
  205.     default:
  206.         return(FALSE);
  207.     }
  208.     default:
  209.     return FALSE;
  210.     }
  211.     return(TRUE);
  212.  
  213. } /* end dlgfnopen */
  214.  
  215. /*=============================================================================
  216.  DLGCHECKOKENABLE enables the ok button in dialog box if and only if the edit
  217.  field contains text.
  218. =============================================================================*/
  219. static VOID NEAR DlgCheckOkEnable(hwnd, idEdit, message)
  220. HWND    hwnd;
  221. int    idEdit;
  222. unsigned message;
  223. {
  224.     if (message == EN_CHANGE) {
  225.     EnableWindow(GetDlgItem(hwnd, IDOK),
  226.          (BOOL)SendMessage(GetDlgItem(hwnd,idEdit),WM_GETTEXTLENGTH,0,0L));
  227.     }
  228. }
  229.  
  230. /*=============================================================================
  231.  DLGADDCORRECTEXTENSION adds appropriate extension to filename, partial filename,
  232.  search spec, or partial search spec.
  233. ==============================================================================*/
  234. static void NEAR DlgAddCorrectExtension(szEdit, fSearching)
  235. char    *szEdit;
  236. BOOL    fSearching;
  237. {
  238.     register char    *pchLast;
  239.     register char    *pchT;
  240.     int ichExt;
  241.     BOOL    fDone = FALSE;
  242.     int     cchEdit;
  243.  
  244.     pchT = pchLast = (char *)AnsiPrev((LPSTR)szEdit, 
  245.             (LPSTR)(szEdit + (cchEdit = lstrlen((LPSTR)szEdit))));
  246.  
  247.     if ((*pchLast == '.' && *(AnsiPrev((LPSTR)szEdit, (LPSTR)pchLast)) == '.') 
  248.                                 && cchEdit == 2)
  249.     ichExt = 0;
  250.     else if (*pchLast == '\\' || *pchLast == ':')
  251.     ichExt = 1;
  252.     else {
  253.     ichExt = fSearching ? 0 : 2;
  254.     for (; pchT > szEdit;pchT=(char *)AnsiPrev((LPSTR)szEdit,(LPSTR)pchT)) {
  255.         /* If we're not searching and we encounter a period, don't add
  256.            any extension.  If we are searching, period is assumed to be
  257.            part of directory name, so go ahead and add extension. However,
  258.            if we are searching and find a search spec, do not add any
  259.            extension. */
  260.         if (fSearching) {
  261.         if (*pchT == '*' || *pchT == '?')
  262.             return;
  263.         } else if (*pchT == '.'){
  264.         return;
  265.         }
  266.         /* Quit when we get to beginning of last node. */
  267.         if (*pchT == '\\')
  268.         break;
  269.     }
  270.     /* Special case hack fix since AnsiPrev can not return value less than
  271.        szEdit. If first char is wild card, return without appending. */
  272.     if (fSearching && (*pchT == '*' || *pchT == '?'))
  273.         return;
  274.     }
  275.     lstrcpy((LPSTR)(pchLast+1), (LPSTR)(szExtSave+ichExt));
  276.  
  277. } /* end dlgaddcorrectextension */
  278.  
  279. /*=====    check for legal filename =====*/
  280. static BOOL near DlgCheckFilename(pch)
  281. register char    *pch;
  282. {
  283.     OFSTRUCT    ofT;
  284.     return (OpenFile((LPSTR)pch, (LPOFSTRUCT)&ofT, OF_PARSE) == 0);
  285. }
  286.  
  287. /*====== return TRUE iff 0 terminated str contains  '*' or '?' ======*/
  288. static BOOL NEAR DlgSearchSpec(sz) 
  289. register char     *sz;
  290. {
  291.     for (; *sz;sz++) {
  292.     if (*sz == '*' || *sz == '?')
  293.         return TRUE;
  294.     }
  295.     return FALSE;
  296. }
  297.